package site.marqstree.kotlin.library.good.ui.activity.detail.sku

import android.view.View
import android.widget.TextView
import androidx.core.view.children
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.nex3z.flowlayout.FlowLayout
import site.marqstree.kotlin.library.base.util.rxbus.RxBus
import site.marqstree.kotlin.library.good.R
import site.marqstree.kotlin.library.good.ui.activity.detail.sku.event.EventSkuChange
import site.marqstree.kotlin.library.good.ui.bean.SkuBean


/*
 * 项目名: KotlinMall
 * 包名: site.marqstree.kotlin.library.good.ui.activity.detail.sku
 * 文件名: SkuRvAdapter
 * 创建者: marqstree
 * 创建时间: 2020/3/17 9:50
 * 描述: TODO
 */
//skus:待选sku数组
//mSelectedSkus:默认选中sku
class SkuRvAdapter(val mSkus: MutableList<SkuBean>)
    : BaseQuickAdapter<SkuBean, BaseViewHolder>(
        R.layout.item_sku,mSkus
), View.OnClickListener {

    override fun convert(helper: BaseViewHolder, item: SkuBean) {
        helper.setText(R.id.mSkuGoupTitle,item.title)
        //往FlowLayout中添加TextView
        val fl = helper.getView<FlowLayout>(R.id.mSkusFl)

        item.content.forEachIndexed { index, s ->
            val groupIndex = groupIndexOfSku(item)
            val selected: Boolean = mSkus[groupIndex].selected == index
            val tv = buildLabel(s, selected)
            tv.setTag(R.id.sku_group_index, groupIndex)
            fl.addView(tv)
        }
    }

    private fun groupIndexOfSku(sku:SkuBean):Int{
        mSkus.forEachIndexed { index, skuBean ->
            if(skuBean.id==sku.id)
                return index
        }
        return -1
    }

    //创建TextView
    private fun buildLabel(text: String,selected:Boolean): TextView {
        val textView = TextView(context)
        textView.text = text
        textView.textSize = 12f
        textView.setPadding(10,10,10,10)
        //根据选中状态，设置高亮边框背景
        //textView.setBackgroundResource(R.drawable.sku_item_bg_selector)
        //根据选中状态，设置字体高亮色
        //textView.setTextColor(context.getColorStateList(R.color.text_color_selector))
        //textView.setTextIsSelectable(true)
        //选中TextView，字体和背景高亮
        if(selected) {
            textView.setBackgroundResource(R.drawable.border_corner_all_blue)
            textView.setTextColor(context.getColor(R.color.colorPrimary))
        }else{
            textView.setBackgroundResource(R.drawable.border_corner_all)
            textView.setTextColor(context.getColor(R.color.common_gray))
        }

        textView.setOnClickListener(this)

        //设置TextView是否可选中
        return textView
    }

    override fun onClick(v: View?) {
        (v as TextView).run{
            (parent as FlowLayout).children.forEachIndexed { index, view ->
                if(view===v){
                    val groupIndex:Int = getTag(R.id.sku_group_index) as Int
                    mSkus[groupIndex].selected=index
                    setBackgroundResource(R.drawable.border_corner_all_blue)
                    setTextColor(context.getColor(R.color.colorPrimary))
                    //发送sku修改事件给主页面
                    sendSkuChange()
                }else {
                    (view as TextView).run {
                        setBackgroundResource(R.drawable.border_corner_all)
                        setTextColor(context.getColor(R.color.common_gray))
                    }
                }
            }
        }
    }

    private fun sendSkuChange(){
        var selectedSkus = ""
        mSkus.forEach {
            selectedSkus += it.content.get(it.selected)+","
        }
        selectedSkus = selectedSkus.substring(0,selectedSkus.length-2)

        RxBus.send(
            EventSkuChange(
                mSkus,
                selectedSkus
            )
        )
    }
}